﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using GWA.GriWebServ;
using GWA.Controls;

namespace GWA
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
                Response.Redirect("~/default.aspx");

            TextBox UserName = (TextBox)FindUserControl("UserName");
            TextBox Email = (TextBox)FindUserControl("Email");
            string name = Request.QueryString["name"];
            string email = Request.QueryString["email"];

            if (name != "" && name != null)
                UserName.Text = name;

            if (email != "" && email != null)
                Email.Text = email;
        }

        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            TextBox UserName = (TextBox)FindUserControl("UserName");
            RadioButtonList Role = (RadioButtonList)FindUserControl("rdbRoles");
            MembershipUser user = Membership.GetUser(UserName.Text);
            SaveProfileInformation(user.ProviderUserKey.ToString());
            Roles.AddUserToRole(user.UserName, "Patient");
            FormsAuthentication.SetAuthCookie(UserName.Text, false);
        }

        private void SaveProfileInformation(string UserID)
        {
            TextBox FirstName = (TextBox)FindUserControl("txtFName");
            TextBox LastName = (TextBox)FindUserControl("txtLName");
            TextBox Addr = (TextBox)FindUserControl("txtAddr");
            TextBox City = (TextBox)FindUserControl("txtCity");
            TextBox Zip = (TextBox)FindUserControl("txtZip");
            TextBox Phone = (TextBox)FindUserControl("txtPhone");
            DropDownList State = (DropDownList)FindUserControl("ddlState");
            RadioButtonList Healthcare = (RadioButtonList)FindUserControl("rdbHealthcare");

            Patient pat = new Patient();
            pat.FirstName = FirstName.Text;
            pat.LastName = LastName.Text;
            pat.Address = Addr.Text;
            pat.City = City.Text;
            pat.Zip = Zip.Text;
            pat.Phone = Phone.Text;
            pat.State = State.Text;
            pat.Healthcare = Convert.ToBoolean(Healthcare.SelectedValue.ToString());
            pat.UserID = UserID;

            Service1 webServ = new Service1();
            webServ.SavePatient(pat);

        }

        protected void btnCont_OnClick(object sender, EventArgs e)
        {
            Response.Redirect(LoginPanel.AccessPage("Patient"));
        }

        private Control FindUserControl(string controlName)
        {
            return CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl(controlName);
        }
    }
}
